//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CappedFlooredCmsCoupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/FloatingRateCouponPricer.h>
#include <gen/QL/Indexes/InterestRateIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Cashflows/CappedFlooredCoupon.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::CCappedFlooredCmsCoupon (DateTime paymentDate, Double nominal, DateTime startDate, DateTime endDate, UInt32 fixingDays, Cephei::QL::Indexes::ISwapIndex^ index, Microsoft::FSharp::Core::FSharpOption<Double>^ gearing, Microsoft::FSharp::Core::FSharpOption<Double>^ spread, Microsoft::FSharp::Core::FSharpOption<Double>^ cap, Microsoft::FSharp::Core::FSharpOption<Double>^ floor, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ isInArrears, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer) : CCappedFlooredCoupon(CCappedFlooredCmsCoupon::typeid)
{
    CSwapIndex^ _Cindex;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phCappedFlooredCmsCoupon = NULL;
#endif
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate); //d
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal); //d
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //d
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //d
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        _Cindex = safe_cast<CSwapIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::SwapIndex>& _index = static_cast<boost::shared_ptr<QuantLib::SwapIndex>&> (_Cindex->GetShared ()); 
        QuantLib::Real _gearing = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (gearing) ? (QuantLib::Real)ValueHelper::Convert (gearing->Value) : 1.0); //4
        QuantLib::Spread _spread = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (spread) ? (QuantLib::Spread)ValueHelper::Convert (spread->Value) : 0.0); //4
        QuantLib::Rate _cap = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (cap) ? (QuantLib::Rate)ValueHelper::Convert (cap->Value) : Null<QuantLib::Rate>()); //4
        QuantLib::Rate _floor = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (floor) ? (QuantLib::Rate)ValueHelper::Convert (floor->Value) : Null<QuantLib::Rate>()); //4
        QuantLib::Date _refPeriodStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodStart) ? (QuantLib::Date)ValueHelper::Convert (refPeriodStart->Value) : QuantLib::Date()); //4
        QuantLib::Date _refPeriodEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodEnd) ? (QuantLib::Date)ValueHelper::Convert (refPeriodEnd->Value) : QuantLib::Date()); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>::IsSome::get (dayCounter))
        {
            _CdayCounter = safe_cast<CDayCounter^> (dayCounter->Value);
            _CdayCounter->Lock();
        }
        QuantLib::DayCounter& _dayCounter = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>::IsSome::get (dayCounter) ? static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()) : QuantLib::DayCounter()); //1
        bool _isInArrears = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (isInArrears) ? (bool)ValueHelper::Convert (isInArrears->Value) : false); //4
        _ppCappedFlooredCmsCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCmsCoupon> (new QuantLib::CappedFlooredCmsCoupon ( _paymentDate,  _nominal,  _startDate,  _endDate,  _fixingDays,  _index,  _gearing,  _spread,  _cap,  _floor,  _refPeriodStart,  _refPeriodEnd,  _dayCounter,  _isInArrears ));
        CFloatingRateCouponPricer^ _CQL_Pricer = safe_cast<CFloatingRateCouponPricer^> (QL_Pricer);
        boost::shared_ptr<QuantLib::FloatingRateCouponPricer>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::FloatingRateCouponPricer>&> (_CQL_Pricer->GetShared ());
        (*_ppCappedFlooredCmsCoupon)->setPricer (_QL_Pricer);
        SetCappedFlooredCoupon (boost::dynamic_pointer_cast<QuantLib::CappedFlooredCoupon> (*_ppCappedFlooredCmsCoupon));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::CCappedFlooredCmsCoupon (boost::shared_ptr<QuantLib::CappedFlooredCmsCoupon>& childNative, Object^ owner) : CCappedFlooredCoupon(CCappedFlooredCmsCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCmsCoupon = NULL;
#endif
	_ppCappedFlooredCmsCoupon = &childNative;
    _ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (boost::dynamic_pointer_cast<QuantLib::CappedFlooredCoupon> (*_ppCappedFlooredCmsCoupon));
}
Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::CCappedFlooredCmsCoupon (QuantLib::CappedFlooredCmsCoupon& childNative, Object^ owner) : CCappedFlooredCoupon(CCappedFlooredCmsCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCmsCoupon = NULL;
#endif
	_ppCappedFlooredCmsCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCmsCoupon> (&childNative);
    _ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (boost::dynamic_pointer_cast<QuantLib::CappedFlooredCoupon> (*_ppCappedFlooredCmsCoupon));
    _CappedFlooredCmsCouponOwner = owner;
    _CappedFlooredCouponOwner = owner;
}

Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::CCappedFlooredCmsCoupon (CCappedFlooredCmsCoupon^ copy) : CCappedFlooredCoupon(CCappedFlooredCmsCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCmsCoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCappedFlooredCmsCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCmsCoupon> (copy->GetShared());
        _ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (boost::dynamic_pointer_cast<QuantLib::CappedFlooredCoupon> (*_ppCappedFlooredCmsCoupon));
    }
}
Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::CCappedFlooredCmsCoupon (PLATFORM::Type^ t) : CCappedFlooredCoupon(CCappedFlooredCmsCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCmsCoupon = NULL;
#endif
	if (!t->IsSubclassOf(CCappedFlooredCmsCoupon::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::CCappedFlooredCmsCoupon (QuantLib::Handle<QuantLib::CappedFlooredCmsCoupon>& childNative, Object^ owner)  : CCappedFlooredCoupon(CCappedFlooredCmsCoupon::typeid)
{
	_phCappedFlooredCmsCoupon = &childNative;
	_ppCappedFlooredCmsCoupon = &static_cast<boost::shared_ptr<QuantLib::CappedFlooredCmsCoupon>>(childNative.currentLink());
    _ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (boost::dynamic_pointer_cast<QuantLib::CappedFlooredCoupon> (*_ppCappedFlooredCmsCoupon));
    _CappedFlooredCmsCouponOwner = owner;
}
Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::CCappedFlooredCmsCoupon (QuantLib::Handle<QuantLib::CappedFlooredCmsCoupon> childNative)  : CCappedFlooredCoupon(CCappedFlooredCmsCoupon::typeid)
{
	_phCappedFlooredCmsCoupon = &childNative;
	_ppCappedFlooredCmsCoupon = &static_cast<boost::shared_ptr<QuantLib::CappedFlooredCmsCoupon>>(childNative.currentLink());
    _ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (boost::dynamic_pointer_cast<QuantLib::CappedFlooredCoupon> (*_ppCappedFlooredCmsCoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::CCappedFlooredCmsCoupon (QuantLib::CappedFlooredCmsCoupon childNative)  : CCappedFlooredCoupon(CCappedFlooredCmsCoupon::typeid)
{
#ifdef HANDLE
	_phCappedFlooredCmsCoupon = NULL;
#endif
	_ppCappedFlooredCmsCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCmsCoupon> (new QuantLib::CappedFlooredCmsCoupon (childNative));
    _ppCappedFlooredCoupon = new boost::shared_ptr<QuantLib::CappedFlooredCoupon> (boost::dynamic_pointer_cast<QuantLib::CappedFlooredCoupon> (*_ppCappedFlooredCmsCoupon));
}
#endif

Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::~CCappedFlooredCmsCoupon ()
{
    if (_ppCappedFlooredCmsCoupon != NULL)
    {
	    delete _ppCappedFlooredCmsCoupon;
        _ppCappedFlooredCmsCoupon = NULL;
    }
}
Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::!CCappedFlooredCmsCoupon ()
{
    if (_ppCappedFlooredCmsCoupon != NULL)
    {
	    delete _ppCappedFlooredCmsCoupon;
    }
}
QuantLib::CappedFlooredCmsCoupon& Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::GetReference ()
{
    if (_ppCappedFlooredCmsCoupon == NULL) throw REFNEW NativeNullException ();
	return **_ppCappedFlooredCmsCoupon;
}
boost::shared_ptr<QuantLib::CappedFlooredCmsCoupon>& Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::GetShared ()
{
    if (_ppCappedFlooredCmsCoupon == NULL) throw REFNEW NativeNullException ();
	return *_ppCappedFlooredCmsCoupon;
}
QuantLib::CappedFlooredCmsCoupon* Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::GetPointer ()
{
    if (_ppCappedFlooredCmsCoupon == NULL) throw REFNEW NativeNullException ();
	return &**_ppCappedFlooredCmsCoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CappedFlooredCmsCoupon>& Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::GetHandle ()
{
	if (_phCappedFlooredCmsCoupon == NULL)
	{
		_phCappedFlooredCmsCoupon = new Handle<QuantLib::CappedFlooredCmsCoupon> (*_ppCappedFlooredCmsCoupon);
	}
	return *_phCappedFlooredCmsCoupon;
}
#endif
bool Cephei::QL::Cashflows::CCappedFlooredCmsCoupon::HasNative () 
{
	return (_ppCappedFlooredCmsCoupon != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::ICappedFlooredCmsCoupon^ Cephei::QL::Cashflows::CCappedFlooredCmsCoupon_Factory::Create (DateTime paymentDate, Double nominal, DateTime startDate, DateTime endDate, UInt32 fixingDays, Cephei::QL::Indexes::ISwapIndex^ index, Microsoft::FSharp::Core::FSharpOption<Double>^ gearing, Microsoft::FSharp::Core::FSharpOption<Double>^ spread, Microsoft::FSharp::Core::FSharpOption<Double>^ cap, Microsoft::FSharp::Core::FSharpOption<Double>^ floor, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ isInArrears, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer)
{
    return REFNEW CCappedFlooredCmsCoupon ( paymentDate,  nominal,  startDate,  endDate,  fixingDays,  index,  gearing,  spread,  cap,  floor,  refPeriodStart,  refPeriodEnd,  dayCounter,  isInArrears,  QL_Pricer);
}
